package start;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class Test01 {
    public Test01() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://192.168.1.101:3306/myTest");
        dataSource.setUsername("root");
        dataSource.setPassword("2348");
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        int rowCount = jdbcTemplate.queryForInt("Select count(*) from aTable");
        System.out.println(rowCount);
        List<A> list =
            jdbcTemplate.query("Select id,name from aTable", new RowMapper<A>() {
                public A mapRow(ResultSet rs, int rowNum) throws SQLException {
                    A a = new A();
                    a.setId(rs.getLong("id"));
                    a.setName(rs.getString("name"));
                    return a;
                }
            });
         for (A a : list) {
            System.out.println(a.getId()+":"+a.getName());
        }
    }
}
